/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.cloud.nacos.registry;

import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author xiaojing
 * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnNacosDiscoveryEnabled
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled",
		matchIfMissing = true)
@AutoConfigureAfter({ AutoServiceRegistrationConfiguration.class,
		AutoServiceRegistrationAutoConfiguration.class,
		NacosDiscoveryAutoConfiguration.class })
public class NacosServiceRegistryAutoConfiguration {

	@Bean
	public NacosServiceRegistry nacosServiceRegistry(
			NacosServiceManager nacosServiceManager,
			NacosDiscoveryProperties nacosDiscoveryProperties) {
		return new NacosServiceRegistry(nacosServiceManager, nacosDiscoveryProperties);
	}

	@Bean
	@ConditionalOnBean(AutoServiceRegistrationProperties.class)
	public NacosRegistration nacosRegistration(
			ObjectProvider<List<NacosRegistrationCustomizer>> registrationCustomizers,
			NacosDiscoveryProperties nacosDiscoveryProperties,
			ApplicationContext context) {
		return new NacosRegistration(registrationCustomizers.getIfAvailable(),
				nacosDiscoveryProperties, context);
	}

	@Bean
	@ConditionalOnBean(AutoServiceRegistrationProperties.class)
	public NacosAutoServiceRegistration nacosAutoServiceRegistration(
			NacosServiceRegistry registry,
			AutoServiceRegistrationProperties autoServiceRegistrationProperties,
			NacosRegistration registration) {
		/**
		 * NacosAutoServiceRegistration 继承了 AbstractAutoServiceRegistration
		 * AbstractAutoServiceRegistration 分别实现了接口 AutoServiceRegistration 和 ApplicationListener<WebServerInitializedEvent>
		 * AutoServiceRegistration：是注册中心与 spring cloud 整合规范中规定的，需要注入的接口类型，客户端需要遵循这个规范，实现这个接口，用以完成客户端自动注册
		 * 其最终的实现类是 spring-cloud-starter-alibaba-nacos-discovery 中配置的 NacosAutoServiceRegistration
		 *
		 * ApplicationListener：是 web 容器监听器，其最终实现类是 当 web 容器启动后， 会调用 NacosAutoServiceRegistration 的父类
		 * AbstractAutoServiceRegistration 的 onApplicationEvent() 方法，最终完成客户端的注册
		 *
		 * 综上，nacos 客户端的注册原理其实就是利用了 spring 事件机制完成的
		 * nacos 利用 spring 的事件机制去实现，利用 spring cloud 中对于注册中心的一个标准，该标准就是 spring-cloud-commons 包中规定的
		 * 其中 AbstractAutoServiceRegistration，这个类主要是完成自动注册的一个抽象流程，具体的注册逻辑需要具体的注册中心框架自己实现
		 * 这个类是一个抽象类，同时也实现了监听器接口，并且接口泛型为 WebServerInitializedEvent，作用就是这个类具有了监听器的功能，
		 * 监听的事件为 WebServerInitializedEvent，当监听到这个事件的时候，就会调用其 onApplication() 方法，继而调用 start() 方法
		 * 最终调用 register() 方法完成客户端的注册
		 *
		 */
		return new NacosAutoServiceRegistration(registry,
				autoServiceRegistrationProperties, registration);
	}

}
